﻿--#include "data\config\activity\KnightErrantCfg.lua" once

--协议操作
function OnKnightErrant(sysarg, msgid, packet)
    local Index = DataPack.readByte(packet)
    --print("Index ="..Index)
    if Index == 0 then
        SendKnightErrant(sysarg)
    else
        GetKnightErrantAward(sysarg, Index)
    end
end

--获得章节进度
function SetKnightErrantProgressValue(sysarg, chapterId, progressValue)
    local cfg 
    for idx = 1, #KnightErrantCfg.chaptersAwards do
        local config = KnightErrantCfg.chaptersAwards[idx]
        if config.chapterId == chapterId then
            cfg = config
        end
    end
    
    if not cfg then
        return
    end

    local var = Actor.getStaticVar(sysarg)
    if not var.knightErrantValue then
        var.knightErrantValue = {}
    end
    if not var.knightErrantValue[chapterId] then
        var.knightErrantValue[chapterId] = 0
    end
    var.knightErrantValue[chapterId] = var.knightErrantValue[chapterId] + progressValue
    if not var.knightErrantAward then
        var.knightErrantAward = {}
    end
    for idx = 1, #KnightErrantCfg.chaptersAwards do
        local cfg = KnightErrantCfg.chaptersAwards[idx]
        if cfg.chapterId == chapterId then
            if cfg.progressValue <= var.knightErrantValue[cfg.chapterId] and ( (not var.knightErrantAward[cfg.chapterId])  or var.knightErrantAward[cfg.chapterId] ~= 2 ) then
                var.knightErrantAward[cfg.chapterId] = 1
            end
            break
        end
    end
    SendKnightErrant(sysarg)
end

--发送数据
function SendKnightErrant(sysarg)
    local days = Actor.getDaysSinceCreateRole(sysarg)
    if days > KnightErrantCfg.createRoleLimitDay then
        return
    end
    local var = Actor.getStaticVar(sysarg)
    if not var.knightErrantValue then
        var.knightErrantValue = {}
    end
    if not var.knightErrantAward then
        var.knightErrantAward = {}
    end
    local totalValue = 0
    local pack = DataPack.allocPacket(sysarg, LogicCmd.miscSys.sysId, LogicCmd.miscSys.sub.sKnightErrant)
    if not pack then return end
    local len = #KnightErrantCfg.chaptersAwards
    DataPack.writeByte(pack, len)
    for idx = 1, len do
        local cfg = KnightErrantCfg.chaptersAwards[idx]
        local state = var.knightErrantAward[cfg.chapterId] or 0
        local progress = var.knightErrantValue[cfg.chapterId] or 0
        DataPack.writeByte(pack, cfg.chapterId)
        DataPack.writeByte(pack, progress)
        DataPack.writeByte(pack, state)
        totalValue = totalValue + progress
    end
    DataPack.writeShort(pack, totalValue)
    DataPack.flush(pack)
end

--领取奖励
function GetKnightErrantAward(sysarg, Index)
    local days = Actor.getDaysSinceCreateRole(sysarg)
    if days > KnightErrantCfg.createRoleLimitDay then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.KnightErrant007, ttFlyTip)
        return
    end
    local var = Actor.getStaticVar(sysarg)
    if not var.knightErrantValue then
        var.knightErrantValue = {}
    end
    if not var.knightErrantAward then
        var.knightErrantAward = {}
    end
    local cfg 
    for idx = 1, #KnightErrantCfg.chaptersAwards do
        local config = KnightErrantCfg.chaptersAwards[idx]
        if config.chapterId == Index then
            cfg = config
        end
    end
    
    if not cfg then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.KnightErrant001, ttFlyTip)
        return
    end

    local rewards = cfg.myAwards

    if not rewards then
        return
    end
    
    if days < cfg.createRoleOpenDay then
        Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.KnightErrant006, cfg.createRoleOpenDay), ttFlyTip)
        return
    end

    if not var.knightErrantValue[Index] then
        var.knightErrantValue[Index] = 0
    end

    if (not var.knightErrantAward[Index] or var.knightErrantAward[Index] == 0) and cfg.progressValue > var.knightErrantValue[Index] then
        Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.KnightErrant005, cfg.progressValue), ttFlyTip)
        return
    end    

    if not ( (var.knightErrantAward[Index] and  var.knightErrantAward[Index] == 1) 
        or ((not var.knightErrantAward[Index] or var.knightErrantAward[Index] == 0) and cfg.progressValue <= var.knightErrantValue[Index])) then
        Actor.sendTipmsg(sysarg, Lang.ScriptTips.KnightErrant002, ttFlyTip)
        return
    end

    var.knightErrantAward[Index] = 2

    local ActorName = Actor.getName(sysarg)
    local noticeStr = string.format(Lang.ScriptTips.KnightErrant008, ActorName, Index)
    System.broadcastTipmsg(noticeStr, ttScreenCenter + ttChatWindow)
    
    if IsBagGridEnough(sysarg, rewards) then
        GiveCommonAward(sysarg, rewards, LogIds.KnightErrantAwardsLogId, Lang.LogTips.KnightErrantAwards)
    else
        local actorId = Actor.getActorId(sysarg)
        local title = string.format(Lang.ScriptTips.KnightErrant003, Index)
        local content = string.format(Lang.ScriptTips.KnightErrant004, Index)

        local sex = Actor.getIntProperty(sysarg, PROP_ACTOR_SEX)
        local vocation = Actor.getIntProperty(sysarg, PROP_ACTOR_VOCATION)

        Awards.GiveToMailById(actorId, rewards, title, content, LogIds.KnightErrantAwardsLogId, Lang.LogTips.KnightErrantAwards, vocation, sex)
    end

    SendKnightErrant(sysarg)
end

function GmKnightErrant(sysarg, chapterId, progressValue)
    SetKnightErrantProgressValue(sysarg, chapterId, progressValue)
end


